package br.unifor.cct.mia.gui.runner;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import weka.gui.explorer.ClassifierPanel;
import br.unifor.cct.mia.coevolution.InvalidTypeException;
import br.unifor.cct.mia.dataenhancement.Database;
import br.unifor.cct.mia.dataenhancement.GenotypeConverter;
import br.unifor.cct.mia.dataenhancement.Structure;
import br.unifor.cct.mia.evaluate.classification.WekaClassification;
import br.unifor.cct.mia.evaluate.classification.WekaClassificationBagging;
import br.unifor.cct.mia.evaluate.classification.WekaClassificationBoosting;
import br.unifor.cct.mia.evaluate.classification.WekaClassifier;
import br.unifor.cct.mia.evolutionary.SpeciesConstants;
import br.unifor.cct.mia.gui.AGMiningClassifierPanelAbstract;
import br.unifor.cct.mia.util.LoadFile;
import br.unifor.cct.mia.util.Utils;

public class ThreadRunner extends Thread {
	
	private AGMiningClassifierPanelAbstract frame;	
	private Structure st;
	private Database db;
	private Integer learnerType;
	private String[] options = null;
	private int specie;
	
	public ThreadRunner(AGMiningClassifierPanelAbstract frame, Structure st, Database db, Integer learnerType,String[] options, int specie) {
		this.frame = frame;
		this.db = db;
		this.learnerType = learnerType;
		this.st = st;
		this.options = options;
		this.specie = specie;
	}
	
	public void run() {
		WekaClassifier classifier = null;
		switch ( specie ) {
			case 12:
				classifier = new WekaClassification(learnerType,options);
				break;
			case 13:
				classifier = new WekaClassificationBagging(learnerType,options);
				break;
			case 14:
				classifier = new WekaClassificationBoosting(learnerType,options);
				break;				
		}
		
		Database dbTeste = LoadFile.loadDatabase(frame.getTestFilePath() );
		
		try {
			File fTrain = new GenotypeConverter().convert(null,SpeciesConstants.FULL_DATABASE,"temp/result.txt",st,db,null,null);
			File fTeste = new GenotypeConverter().convert(null,SpeciesConstants.FULL_DATABASE,"temp/resultTeste.txt",st,dbTeste,null,null);
			
			Date d0 = new Date();
			frame.log("Processando...");	
			double fitness = classifier.evaluate(fTrain);
			frame.log("Processamento Finalizado");
			Date d1 = new Date();
			
			switch ( specie ) {
				case 12:
					classifier = new WekaClassification(learnerType,options);
					break;
				case 13:
					classifier = new WekaClassificationBagging(learnerType,options);
					break;
				case 14:
					classifier = new WekaClassificationBoosting(learnerType,options);
					break;				
			}	
		    //classifier.setTestType(ClassifierPanel.SUPPLIED_TEST_SET);
			double testeFit = classifier.evaluate(fTrain,fTeste);
			
			frame.finalizarExecucao(fitness,testeFit,0,String.valueOf( (d1.getTime()-d0.getTime())/1000 ),true);			
            Utils.clearTemp();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InvalidTypeException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}		
	}

}
